# coding: utf-8
from flask_login import UserMixin
from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()



class BlogUser(db.Model, UserMixin):
    __tablename__ = 'blog_user'

    user_id = db.Column(db.Integer, primary_key=True, info='用户表的ID')
    login_name = db.Column(db.String(18, 'utf8mb4_general_ci'), nullable=False, info='用户名')
    user_pwd = db.Column(db.String(18, 'utf8mb4_general_ci'), nullable=False, info='用户密码')
    locked = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='锁定状态字段(0-正常  1- 冻结 2-黑名单 )')
    role = db.Column(db.String(20, 'utf8mb4_general_ci'), nullable=False, server_default=db.FetchedValue(), info='角色 member(成员) < creator(创作者) < ambassador(大使) < ...... < root(最高权限)')
    is_deleted = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='0-正常  1-已注销')
    create_time = db.Column(db.DateTime, nullable=False)
    # toString方法
    def __repr__(self):
        return '用户名 %r' % self.login_name


class DraftArticle(db.Model):
    __tablename__ = 'draft_articles'

    draft_id = db.Column(db.Integer, primary_key=True, info='草稿文章ID')
    title = db.Column(db.String(120, 'utf8mb4_general_ci'), nullable=False, info='文章标题')
    content = db.Column(db.String(collation='utf8mb4_general_ci'), nullable=False, info='文章内容')
    cover_img = db.Column(db.String(200, 'utf8mb4_general_ci'), server_default=db.FetchedValue(), info='封面图url(默认空字符串')
    tag = db.Column(db.String(60, 'utf8mb4_general_ci'), server_default=db.FetchedValue(), info='标签(-附属表的名字')
    intro = db.Column(db.String(255, 'utf8mb4_general_ci'), nullable=False, info='简介')
    create_time = db.Column(db.DateTime)
    update_time = db.Column(db.DateTime)
    user_id = db.Column(db.ForeignKey('blog_user.user_id', ondelete='CASCADE'), nullable=False, index=True, info='外键-用户表的ID')

    user = db.relationship('BlogUser', primaryjoin='DraftArticle.user_id == BlogUser.user_id', backref='draft_articles')
    # toString方法
    def __repr__(self):
        return '草稿题 %r' % self.title


class MultiArticle(db.Model):
    __tablename__ = 'multi_articles'

    article_id = db.Column(db.Integer, primary_key=True, info='博客文章ID')
    title = db.Column(db.String(120, 'utf8mb4_general_ci'), nullable=False, info='文章标题')
    content = db.Column(db.String(collation='utf8mb4_general_ci'), nullable=False, info='文章内容')
    cover_img = db.Column(db.String(200, 'utf8mb4_general_ci'), server_default=db.FetchedValue(), info='封面图url(默认空字符串')
    tag = db.Column(db.String(60, 'utf8mb4_general_ci'), index=True, server_default=db.FetchedValue(), info='标签(外键-附属表的名字')
    intro = db.Column(db.String(255, 'utf8mb4_general_ci'), nullable=False, info='简介')
    create_time = db.Column(db.DateTime, nullable=False)
    update_time = db.Column(db.DateTime, nullable=False)
    class_sub_id = db.Column(db.ForeignKey('multi_class_sub.class_sub_id', ondelete='CASCADE'), nullable=False, index=True, info='外键-多级分类附属表ID')

    class_sub = db.relationship('MultiClassSub', primaryjoin='MultiArticle.class_sub_id == MultiClassSub.class_sub_id', backref='multi_articles')

    # toString方法
    def __repr__(self):
        return '文章标题 %r' % self.title


class MultiClassSub(db.Model):
    __tablename__ = 'multi_class_sub'

    class_sub_id = db.Column(db.Integer, primary_key=True, info='多级分类附属表ID')
    class_sub_name = db.Column(db.String(65, 'utf8mb4_general_ci'), nullable=False, index=True, info='分类附属的名字')
    create_time = db.Column(db.DateTime, nullable=False)
    intro = db.Column(db.String(255, 'utf8mb4_general_ci'), nullable=False, info='简介')

    # toString方法
    def __repr__(self):
        return '文章类名 %r' % self.class_sub_name


class PraiseArticle(db.Model):
    __tablename__ = 'praise_article'

    praise_id = db.Column(db.Integer, primary_key=True, info='文章点赞表ID')
    is_praise = db.Column(db.Integer, nullable=False, info='(0没点, 1点了)')
    article_id = db.Column(db.ForeignKey('multi_articles.article_id', ondelete='CASCADE'), nullable=False, index=True, info='外键-博客文章ID')
    visitor_id = db.Column(db.ForeignKey('visitors.visitor_id', ondelete='CASCADE'), nullable=False, unique=True, info='外键-游客ID')

    article = db.relationship('MultiArticle', primaryjoin='PraiseArticle.article_id == MultiArticle.article_id', backref='praise_articles')
    visitor = db.relationship('Visitor', primaryjoin='PraiseArticle.visitor_id == Visitor.visitor_id', backref='praise_articles')

    # toString方法
    def __repr__(self):
        return '文章点赞ID %r' % self.praise_id


class PraiseTool(db.Model):
    __tablename__ = 'praise_tool'

    praise_id = db.Column(db.Integer, primary_key=True, info='工具点赞表ID')
    is_praise = db.Column(db.Integer, nullable=False, info='(0没点, 1点了)')
    tool_page_id = db.Column(db.ForeignKey('tools_net_page.tool_page_id', ondelete='CASCADE'), nullable=False, index=True, info='外键-工具页面的ID')
    visitor_id = db.Column(db.ForeignKey('visitors.visitor_id', ondelete='CASCADE'), nullable=False, unique=True, info='外键-游客ID')

    tool_page = db.relationship('ToolsNetPage', primaryjoin='PraiseTool.tool_page_id == ToolsNetPage.tool_page_id', backref='praise_tools')
    visitor = db.relationship('Visitor', primaryjoin='PraiseTool.visitor_id == Visitor.visitor_id', backref='praise_tools')

    # toString方法
    def __repr__(self):
        return '工具点赞ID %r' % self.praise_id



class Speak(db.Model):
    __tablename__ = 'speaks'

    speak_id = db.Column(db.Integer, primary_key=True, info='留言表ID')
    nick = db.Column(db.String(30, 'utf8mb4_general_ci'), nullable=False, server_default=db.FetchedValue(), info='昵称(默认匿名')
    contact = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='联系方式(默认0邮箱，1手机号')
    content = db.Column(db.Text(collation='utf8mb4_general_ci'), nullable=False, info='详细说明内容文本')
    create_time = db.Column(db.DateTime)
    simple_conten = db.Column(db.String(100, 'utf8mb4_general_ci'), nullable=False, server_default=db.FetchedValue(), info='简单说明（默认空字符串')
    visitor_id = db.Column(db.ForeignKey('visitors.visitor_id'), nullable=False, index=True, info='外键-游客ID')

    visitor = db.relationship('Visitor', primaryjoin='Speak.visitor_id == Visitor.visitor_id', backref='speaks')

    # toString方法
    def __repr__(self):
        return '留言表ID %r' % self.speak_id


class Sponsor(db.Model):
    __tablename__ = 'sponsors'

    sponsor_id = db.Column(db.Integer, primary_key=True, info='赞助ID')
    contact = db.Column(db.String(50, 'utf8mb4_general_ci'), server_default=db.FetchedValue(), info='联系方式(默认空字符串')
    nick = db.Column(db.String(50, 'utf8mb4_general_ci'), server_default=db.FetchedValue(), info='名字也可以是网名(默认空字符串')
    image_head = db.Column(db.String(255, 'utf8mb4_general_ci'), server_default=db.FetchedValue(), info='头像的链接网址(默认空字符串')
    url_link = db.Column(db.String(255, 'utf8mb4_general_ci'), info='展示网址(默认空字符串')
    is_show = db.Column(db.String(255, 'utf8mb4_general_ci'), nullable=False, server_default=db.FetchedValue(), info='(1显示 or 0不显示)')
    gold_num = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='金额（int类型')
    create_time = db.Column(db.DateTime)

    # toString方法
    def __repr__(self):
        return '赞助ID %r' % self.sponsor_id


class ToolsNet(db.Model):
    __tablename__ = 'tools_net'

    tools_id = db.Column(db.Integer, primary_key=True, info='所有工具的ID')
    tools_name = db.Column(db.String(60, 'utf8mb4_general_ci'), nullable=False, info='工具名')
    create_time = db.Column(db.DateTime)
    intro = db.Column(db.String(255, 'utf8mb4_general_ci'), nullable=False, info='简介')

    # toString方法
    def __repr__(self):
        return '工具类名 %r' % self.tools_name


class ToolsNetPage(db.Model):
    __tablename__ = 'tools_net_page'

    tool_page_id = db.Column(db.Integer, primary_key=True, info='工具页面的ID')
    title = db.Column(db.String(120, 'utf8mb4_general_ci'), nullable=False, info='页面标题')
    content = db.Column(db.String(collation='utf8mb4_general_ci'), nullable=False, info='网页内容html')
    tag = db.Column(db.String(60, 'utf8mb4_general_ci'), server_default=db.FetchedValue(), info='标签')
    intro = db.Column(db.String(255, 'utf8mb4_general_ci'), nullable=False, info='简介')
    create_time = db.Column(db.DateTime)
    tools_id = db.Column(db.ForeignKey('tools_net.tools_id', ondelete='CASCADE'), nullable=False, index=True, info='外键-在线工具ID')

    tools = db.relationship('ToolsNet', primaryjoin='ToolsNetPage.tools_id == ToolsNet.tools_id', backref='tools_net_pages')

    # toString方法
    def __repr__(self):
        return '工具标题 %r' % self.title


class Visitor(db.Model):
    __tablename__ = 'visitors'

    visitor_id = db.Column(db.Integer, primary_key=True, info='游客ID')
    visitor_id_address = db.Column(db.String(50, 'utf8mb4_general_ci'), nullable=False, unique=True, info='游客本机ID地址')
    os_type = db.Column(db.String(20, 'utf8mb4_general_ci'), nullable=False, info='设备系统类型')
    create_time = db.Column(db.DateTime, nullable=False)
    region = db.Column(db.String(100, 'utf8mb4_general_ci'), nullable=False, info='所在的地区')

    # toString方法
    def __repr__(self):
        return '游客ip地址 %r' % self.visitor_id_address

